package zjc.bookkeeping.minepage.dAccountbookmanagement;

import static androidx.constraintlayout.helper.widget.MotionEffect.TAG;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputFilter;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import zjc.bookkeeping.R;
import zjc.bookkeeping.activity.MainActivity;
import zjc.bookkeeping.db.AccountBean;
import zjc.bookkeeping.db.ConsumptionBean;
import zjc.bookkeeping.db.DBManager;
import zjc.bookkeeping.db.UserBean;
import zjc.bookkeeping.minepage.cClassificationmanagement.ClassificationmanagementMainActivity;
import zjc.bookkeeping.server.StaticData;
import zjc.bookkeeping.service.AccountService;
import zjc.bookkeeping.service.ConsumptionService;
import zjc.bookkeeping.service.imp.AccountServiceImp;
import zjc.bookkeeping.service.imp.ConsumptionServiceImp;
import zjc.bookkeeping.tools.PercentLinearLayout;
import zjc.bookkeeping.tools.ToolsClass;

public class AccountbookmanagementMainActivity extends Activity implements View.OnClickListener {
    List<AccountBean> accountBeanList;
    ImageButton backFinish;
    Button addAccount,joinAccount;
    ListView listView;
    accountbookListAdapter adapter;
    int selectPos = DBManager.selectPos();
    AccountService accountService = new AccountServiceImp();
    ConsumptionService consumptionService = new ConsumptionServiceImp();
    UserBean userBean = new UserBean();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        setContentView(R.layout.activity_accountbookmanagement_main);
        userBean = DBManager.findUser();
        accountBeanList = DBManager.getAllAccount();
        initView();
        loadDataToList();
    }

    private void loadDataToList() {
        accountBeanList = DBManager.getAllAccount();
        adapter = new accountbookListAdapter(AccountbookmanagementMainActivity.this,accountBeanList);
        listView.setAdapter(adapter);
    }

    private void initView() {
        backFinish = findViewById(R.id.account_back_btn);
        addAccount = findViewById(R.id.account_add_btn);
        listView = findViewById(R.id.account_list_lv);
        joinAccount = findViewById(R.id.account_join_btn);
        //点击切换账本
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                if (position != selectPos){
                    AlertDialog.Builder builder = new AlertDialog.Builder(AccountbookmanagementMainActivity.this);
                    builder.setMessage("切换账本？");
                    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            //保存选中的账本和账本id
//                            StaticData.setSelectPos(position);
//                            StaticData.setAccountId(accountBeanList.get(position).getAccount_id());
                            if (accountBeanList.get(position).getAccount_kind() == 1){
                                ToolsClass.showToast(AccountbookmanagementMainActivity.this,"多人账本数据显示暂未开发，暂时不支持切换多人账本~", Toast.LENGTH_SHORT);
                            }else {
                                DBManager.insertSelectAccount(position,accountBeanList.get(position).getAccount_id());
                                finish();
                            }
                        }
                    });
                    builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {

                        }
                    });
                    builder.create().show();
                }
            }
        });
        //长按删除账本
        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long l) {
                if (accountBeanList.get(position).getAccount_kind() == 0){
                    showDelete(position);
                }else {
                    showSelect(position);
                }
                return true;
            }
        });
        backFinish.setOnClickListener(this);
        addAccount.setOnClickListener(this);
        joinAccount.setOnClickListener(this);
    }

    private void showSelect(int position) {
        AlertDialog.Builder builder = new AlertDialog.Builder(AccountbookmanagementMainActivity.this);
        builder.setMessage("多人账单管理");
        builder.setPositiveButton("删除", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                showDelete(position);
            }
        });
        builder.setNegativeButton("详情", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Intent intent = new Intent(AccountbookmanagementMainActivity.this,MultiAccountActivity.class);
                MultiAccountActivity.name = accountBeanList.get(position).getAccount_name();
                MultiAccountActivity.id = accountBeanList.get(position).getAccount_key();
                startActivity(intent);
            }
        });
        builder.create().show();
    }

    private void showDelete(int position) {
        AlertDialog.Builder builder = new AlertDialog.Builder(AccountbookmanagementMainActivity.this);
        builder.setMessage("确定删除账本和账本记录？");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                if (accountBeanList.size() > 1){
                    if (position < selectPos){
                        int k = selectPos - 1;
//                                StaticData.setSelectPos(k);
                        Log.i("k",k+"--------");
                        int accountId = DBManager.selectAccountId();
                        Log.i("id是",accountId+"————————————");
                        DBManager.insertSelectAccount(k,accountId);
                    }
                    if (position == selectPos && position == accountBeanList.size()-1){
                        int k = selectPos - 1;
//                                StaticData.setSelectPos(k);
                        Log.i("k",k+"--------");
                        int accountId = accountBeanList.get(k).getAccount_id();
                        Log.i("id是",accountId+"————————————");
                        DBManager.insertSelectAccount(k,accountId);
                    }
                    int id = accountBeanList.get(position).getAccount_id();
                    adapter.remove(position);
                    String uid = DBManager.finUidById(id);
                    accountService.deleteAccount(uid);
                    consumptionService.deleteConsumption(uid,userBean.getUser_id());
                    DBManager.deleteAccountbyid(id);
                }else {
                    ToolsClass.showToast(AccountbookmanagementMainActivity.this,"至少保留一个账本。", Toast.LENGTH_SHORT);
                }
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

            }
        });
        builder.create().show();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.account_back_btn:
                finish();
                break;
            case R.id.account_add_btn:
                showAddAccountDialog();
                break;
            case R.id.account_join_btn:
                ToolsClass.showToast(AccountbookmanagementMainActivity.this,"该功能暂未开发~", Toast.LENGTH_SHORT);
                break;
        }
    }

    private void showAddAccountDialog() {
        AddbookDialog addbookDialog = new AddbookDialog(AccountbookmanagementMainActivity.this);
        addbookDialog.show();
        addbookDialog.setDialogSize();
        addbookDialog.setAddAccountSure(new AddbookDialog.AddAccountSure(){
            @Override
            public void onSureListener() {
                loadDataToList();
            }
        });
    }
}